﻿using AllIsWell.Tests.TestData;
using Moq;
using NUnit.Framework;

namespace AllIsWell.Services.Tests.PageServiceTests
{
    [TestFixture]
    class CreatePage : PageServiceTest
    {
        [Test]
        public void ShouldAddPageInPageRepository()
        {
            var page = PageSeed.GetPage();
            PageRepositoryMock.Setup(repo => repo.AddPage(page)).Verifiable();

            PageService.CreatePage(page);

            PageRepositoryMock.Verify(repo => repo.AddPage(page));
        }

        [Test]
        public void ShouldSaveAfterAddingThePageInRepository()
        {
            int[] callOrder = {0};
            var page = PageSeed.GetPage();
            PageRepositoryMock.Setup(repo => repo.AddPage(page))
                              .Callback(() => Assert.AreEqual(0, callOrder[0]++, "Page should be added before calling save"));
            UnitOfWorkMock.Setup(uow => uow.Save())
                          .Callback(() => Assert.AreEqual(1, callOrder[0]++, "Save should be called after adding the page"));

            PageService.CreatePage(page);

            PageRepositoryMock.Verify(repo => repo.AddPage(page), Times.Once());
            UnitOfWorkMock.Verify(uow => uow.Save(), Times.Once());
        }

        [Test]
        public void ShouldReturnThePageReturnedByTheRepository()
        {
            var expectedPage = PageSeed.GetPage();
            PageRepositoryMock.Setup(repo => repo.AddPage(expectedPage)).Returns(expectedPage);

            var actualPage = PageService.CreatePage(expectedPage);

            Assert.AreEqual(expectedPage, actualPage);
        }
    }
}